import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil from '../../../data/common/appScene';
import SessionUtil from "../../../data/session/session"
const Common = require('../../../utils/common.js');

Page({
    data: {
        token: '',
        ItemList: {},
        chosenNum: 1,
        pageNo: 1,
        name: '',
        isloading: true,
        nomore: false,
        moretext: "加载更多",
        tagValues: 0,
        Details: true,
        attrType:"",
        classarray:{},
        classlist:"",
        brandarray:{},
        brandlist:"",
        catearray:{},
        catelist:"",
        btem:{},
        catem:{},
        cltem:{},
        hascate:false,
        hasbrand:false,
        hasclass:false,
        count:0
    },
    onLoad: function (options) {
        // 开启转发按钮
        PagePromoterUtil.showShareMenu(true).then()
        SessionUtil.getInstance().then(session => {
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this)
            let server = session.getTheSellingServerSync()
            wx.setNavigationBarTitle({
                title: server.getTheAgentSync().showName + '酒库',
            })
            this.ItemList();
            Common.w_widths(this);
            let img_height = this.data.w_width / 2 - 20;
            this.setData({
                img_height: img_height
            })
            this.getAttribute();
        })
    },
    onShow: function(){
        SessionUtil.getInstance().then(()=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this, false)
        })
    },
    /**筛选接口 */
    getAttribute(){
        var page = this;
        Common.my_request({
            url: '/AppMaster/Product/getAttributeList',
            success: function (res) {
                console.log(res);
                if (res.data.result == 0) {
                    page.setData({
                        attrlist : res.data.data
                    })
                } else {
                    Common.if_login2(res.data.message);
                }

            }
        })
    },
    
    // 更新搜索关键词
    goSearch: function (e) {
        var page = this;
        var KEY = page.data.name;
        this.setData({
            pageNo: 1,
        })
        page.setData({
            ItemList: "",
        })
        this.ItemList();
    },

    /**array->字符串 */
    bindArray(array, list, callback){
        for (var i in array) {
            if (array[i]) {
                list += i + ",";
            }
        }
        var ishighlight = false;
        if (list.length > 0) {
            ishighlight = true
        }
        var thislist = list.substring(0, list.length - 1);
        var combo = {};
        combo.ishighlight = ishighlight;
        combo.thislist = thislist;
        console.log(combo)
        callback(combo);
    },

    /**确定选择属性 */
    getlist: function (e) {
        var page = this;
        page.setData({
            pageNo: 1,
            ItemList : "",
        })

        var catelist = "",
            brandlist = "",
            classlist = "";
        var attrType = page.data.attrType;
        if (attrType == "category") {
            page.bindArray(page.data.catearray, catelist, function (combo){
                page.setData({
                    hascate: combo.ishighlight,
                    catelist: combo.thislist,
                })
            });
        }
        if (attrType == "brand") {
            page.bindArray(page.data.brandarray, brandlist, function (combo) {
                page.setData({
                    hasbrand: combo.ishighlight,
                    brandlist: combo.thislist,
                })
            });
            
        }
        if (attrType == "class") {
            page.bindArray(page.data.classarray, classlist, function (combo) {
                page.setData({
                    hasclass: combo.ishighlight,
                    classlist: combo.thislist,
                })
            });
        }
        
        this.ItemList();
        page.setData({
            is_closePop:false
        });
    },

    /**重置属性 */
    resetAttr(){
        var page = this;
        var attrType = page.data.attrType;
        if (attrType == "category"){
            page.setData({
                catearray: {},
                catelist:""
            })
        }
        if (attrType == "brand") {
            page.setData({
                brandarray: {},
                brandlist:""
            })
        }
        if (attrType == "class") {
            page.setData({
                classarray: {},
                classlist:""
            })
        }
    },

    /**商品列表接口 */
    ItemList: function (action = '') {
        wx.showLoading({
            title: '数据加载中',
        })
        var page = this;
        var nowData = page.data.ItemList;
        var newData = {};
        var pageNo = page.data.pageNo;
        var keyword = page.data.name;
        
        page.setData({
            isloading: false,
            moretext: "加载更多",
            nomore: true
        });
        if (pageNo < 1) {
            page.setData({
                isloading: true,
                nomore: false,
                moretext: "没有更多了"
            });
            wx.hideLoading();
            return;
        }

        Common.my_request({
            url: '/AppMaster/Product/productList/',
            data: {
                keyword: keyword,
                pageIndex: pageNo,
                pageSize: 10,
                brand_attribute_value_id: page.data.brandlist,
                class_attribute_value_id: page.data.classlist,
                product_category_id: page.data.catelist,
            },
            success: function (res) {
                console.log(res);
                if (res.data.result == 0) {

                    pageNo++;

                    if (res.data) {
                        var Lists = res.data.data.list;
                        if (Lists.length) {
                            var newData = Lists;
                        } else {
                            pageNo = -100;
                            page.setData({
                                isloading: true,
                                nomore: false,
                                moretext: "没有更多了"
                            });
                        }
                    }

                    if (action == 'Refresh') {
                        console.log('is Refresh!');                        
                        wx.stopPullDownRefresh();
                    } else if (action == 'Page') {
                        console.log('is Page!');
                        if (nowData.length) {
                            var newData = nowData.concat(Lists)
                        } else {
                            var newData = Lists
                        }
                    }
                    page.setData({
                        count: res.data.data.count,
                        ItemList: newData,
                        pageNo: pageNo,
                        isloading: true,
                        nomore: false,
                    });

                    // 关闭加载
                    wx.hideLoading();


                } else {
                    console.log(res.data.message);
                    Common.if_login2(res.data.message);
                }

            }
        });
    },
    getNormalchose(list){
        var temarray, temjson = {};
        if (list) {
            temarray = list.split(",");
            for (var i = 0; i < temarray.length; i++) {
                temjson[temarray[i]] = true;
            }
        }
        return temjson;
    },

    /**打开属性弹窗 */
    OrderBy: function (e) {
        console.log(4156)
        var page = this;
        var otype = e.currentTarget.id;
        if (otype == "brand"){
            page.setData({
                btem: page.getNormalchose(page.data.brandlist)
            });
        }
        if (otype == "class") {
            page.setData({
                cltem: page.getNormalchose(page.data.classlist)
            });
        }
        if (otype == "category") {
            page.setData({
                catem: page.getNormalchose(page.data.catelist)
            });
        }

        page.setData({
            catearray: page.data.catem,
            brandarray: page.data.btem,
            classarray: page.data.cltem,
            attrType: otype,
            is_closePop:true,
        });
    },

    /**选择属性 */
    chose(e){
        var page = this,
            id = e.currentTarget.id,
            otype = page.data.attrType,
            array = {}, list = [];
        if (otype == 'category'){
            array = page.data.catearray;
            if (array[id]) {
                array[id] = false;
            } else {
                array[id] = true;
            }
            page.setData({
                catearray : array,
            })
        }
        if (otype == 'brand') {
            array = page.data.brandarray;
            if (array[id]) {
                array[id] = false;
            } else {
                array[id] = true;
            }
            page.setData({
                brandarray: array,
            })
        }
        if (otype == 'class') {
            array = page.data.classarray;
            if (array[id]) {
                array[id] = false;
            } else {
                array[id] = true;
            }
            page.setData({
                classarray: array,
            })
        }
    },

    /**更新搜索框 */
    updateSearchKey: function (e) {
        this.setData({
            name: e.detail.value
        })
    },

    /**关闭属性弹窗 */
    closePop() {
        var page = this;
        var otype = page.data.attrType;
        if (otype == "brand") {
            page.setData({
                brandarray: page.data.btem,
            });
        }
        if (otype == "class") {
            page.setData({
                classarray: page.data.cltem,
            });
        }
        if (otype == "category") {
            page.setData({
                catearray: page.data.catem,
            });
        }

        page.setData({
            is_closePop: false
        })
    },

    /**下拉刷新 */
    onPullDownRefresh: function () {
        var page = this;
        page.setData({
            ItemList: {},
            pageNo: 1
        });
        page.ItemList('Refresh');
    },

    /**上拉展示更多 */
    onReachBottom: function () {
        var page = this;
        var pageNo = page.data.pageNo;
        page.setData({
            pageNo: pageNo
        });
        page.ItemList('Page');
    },

    /**去详情页 */
    goDetail: function (e) {
        var itemId = e.currentTarget.id;
        this.setData({
            Details: false,
        })
        wx.navigateTo({
            url: '/pages/liquorstore/detail/detail?itemId=' + itemId
        })
    },

    /**分享 */
    onShareAppMessage: function () {
        let agentName = '海量酒库',server = SessionUtil.getInstanceSync().getTheSellingServerSync()
        if(server){
            agentName = server.getTheAgentSync().showName + '酒库'
        }
        // 构造并返回自定义数据
        return (new PagePromoter( this.route, { itemId : this.data.itemId }))
        .setShareTitle(agentName)
        .buildShareInfo()
    },
})